package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"im/global"
	"im/models"
)

type VideoController struct {
	beego.Controller
}

func (v *VideoController) GetVideoFind() {
	id, _ := v.GetInt("id")

	var video models.Video

	search, err := video.Search(id)
	if err != nil {
		v.Data["json"] = global.Response{
			Code:    -1,
			Message: "数据内部错误",
			Data:    nil,
		}

		v.ServeJSON()
		return
	}

	v.Data["json"] = global.Response{
		Code:    0,
		Message: "数据查询OK",
		Data:    search,
	}

	v.ServeJSON()
}

func (v *VideoController) GetVideoSort() {

	videoSort, _ := v.GetInt("video_sort")

	var video models.Video

	sort, err := video.Sort(videoSort)
	if err != nil {

		v.Data["json"] = global.Response{
			Code:    -1,
			Message: "数据内部错误",
			Data:    video,
		}

		v.ServeJSON()
		return
	}

	v.Data["json"] = global.Response{
		Code:    0,
		Message: "数据分类OK",
		Data:    sort,
	}

	v.ServeJSON()
}

func (v *VideoController) GetVideoList() {
	title := v.GetString("title")

	var video models.Video

	list, res, err := video.List(title)
	if err != nil {
		v.Data["json"] = global.Response{
			Code:    -1,
			Message: "数据内部错误",
			Data:    nil,
		}

		v.ServeJSON()
		return
	}

	if res.Error != nil {

		v.Data["json"] = global.Response{
			Code:    -2,
			Message: "数据内部错误",
			Data:    nil,
		}

		v.ServeJSON()
		return
	}

	v.Data["json"] = global.Response{
		Code:    0,
		Message: "数据搜索OK",
		Data:    list,
	}

	v.ServeJSON()
}
